package com.unionfetch.content_home.ui.view

import android.content.Context
import android.text.TextUtils
import android.transition.TransitionManager
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.unionfetch.content_home.R
import com.unionfetch.content_home.bean.UserStatus
import com.unionfetch.content_home.bean.findUserStatus
import com.unionfetch.content_home.ui.OpenAccountGuideUtil
import com.unionfetch.content_home.ui.auth.AddressAuthActivity
import com.unionfetch.content_home.ui.auth.IdentityAuthActivity
import com.unionfetch.content_home.ui.auth.SumsubManager
import com.unionfetch.content_home.ui.step.FirstStepOpenAccountActivity
import com.unionfetch.content_home.ui.step.SecondStepOpenAccountActivity
import com.unionfetch.content_home.ui.step.ThirdStepOpenAccountActivity
import com.unionftech.common.R.color
import com.unionftech.common.R.string
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.bean.SystemUserInfo
import com.unionftech.common.common.bean.UserKYCFileInfo
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.ActivityCollector
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.Utils

class OpenAccountStateView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attrs, defStyleAttr) {
    private val mContext = context
    private val mTvOpenAccountStateTip by lazy { findViewById<TextView>(R.id.tv_open_account_state_tip) }
    private val mIvStateStep by lazy { findViewById<ImageView>(R.id.iv_state_step) }
    private val mBtnOpenAccountNow by lazy { findViewById<Button>(R.id.btn_open_account_now) }


    init {
        LayoutInflater.from(mContext)
            .inflate(R.layout.layout_wv_open_account_state_view, this, true)
        initLanguageTextForLabel()
    }


    private fun initLanguageTextForLabel() {
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.basic_info_create_account)
        mBtnOpenAccountNow.text =
            DynamicResourceManager.getString(mContext, string.open_account_now)
    }


    /**
     * 更新直客的UI
     */
    fun updateRC(userInfo: SystemUserInfo, kycFileInfo: UserKYCFileInfo) {
        if (userInfo.status < UserStatus.USER_WAITTING.status) {
            //展示立即开户
            updateRCOpenAccountStepUI()
        } else if (userInfo.status == UserStatus.USER_FAILURE.status) {
            //拒绝
            updateRefuseUI()
        } else {
            //身份认证
            if (TextUtils.isEmpty(userInfo.filesStatus) || userInfo.filesStatus == "00") {
                //POI和POA都没有填写
                updatePoiAuth()
            } else if (userInfo.filesStatus == "10") {
                if (!kycFileInfo.poiHaveResult) {
                    //身份证明审核中
                    updatePoiWaiting()
                } else {
                    //补充地址证明
                    updatePoaAuth()
                }
            } else if (userInfo.filesStatus == "11") {
                if (!updateKycFileInfo(kycFileInfo)) {
                    //身份证明和地址证明都完成上传，之后判断当前的账号状态
                    if (userInfo.status == UserStatus.USER_WAITTING.status) {
                        //展示审核中
                        updateWaitingUI()
                    } else if (userInfo.status == UserStatus.USER_SUCCESS.status) {
                        //开户成功，身份认证完成
                        visibility = GONE
                    }
                }
            }
        }
    }

    /**
     * 更新IB开户步骤
     */
    fun updateIB(userInfo: SystemUserInfo) {
        //没有交易账号，没有在审核中的交易账号, 没有账户(展示立即开户)
        if (!userInfo.hasAccount && !userInfo.hasAudit && userInfo.hasAuditMain == -1) {
            //立即开户
            updateOpenStepIB()
        } else if (!userInfo.hasAccount && userInfo.hasAudit) {
            //没有交易账号,有在审核中的交易账号(审核中)
            //审核中
            updateWaitingUI()
        } else {
            if (userInfo.status == UserStatus.USER_SUCCESS.status) {
                //审核成功
                visibility = GONE
            } else if (userInfo.status == UserStatus.USER_FAILURE.status) {
                //审核失败
                //ib: 没有审核失败的状态, 审核失败走立即开户的逻辑
                updateOpenStepIB()
            }
        }
    }

    /**
     * 接口失败的UI
     */
    fun updateFailed() {
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.basic_info_create_account)
        mBtnOpenAccountNow.text =
            DynamicResourceManager.getString(mContext, string.open_account_now)
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.setOnClickListener(null)
    }

    private fun updateRefuseUI() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        mIvStateStep.setImageResource(R.drawable.ic_open_account_failed)
        mTvOpenAccountStateTip.text = DynamicResourceManager.getString(
            mContext, string.audit_failed
        )
        mBtnOpenAccountNow.visibility = GONE
    }


    private fun updateOpenStepIB() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mIvStateStep.setImageResource(R.drawable.ic_open_account_step_1)
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.basic_info_create_account)
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.text =
            DynamicResourceManager.getString(mContext, string.open_account_now)
        mBtnOpenAccountNow.setOnClickListener {
            SecondStepOpenAccountActivity.startThis(mContext, true)
        }
    }

    /**
     * 更新直客开户步骤 UI展示
     */
    private fun updateRCOpenAccountStepUI() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mIvStateStep.setImageResource(R.drawable.ic_open_account_step_1)
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.basic_info_create_account)
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.text =
            DynamicResourceManager.getString(mContext, string.open_account_now)
        mBtnOpenAccountNow.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                OpenAccountGuideUtil.gotoOpenAccountStep()
            }
        }
    }


    /**
     * 更新地址认证UI（补料）
     */
    private fun fillPoaAuthUI() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mTvOpenAccountStateTip.text = DynamicResourceManager.getString(
            mContext, string.supply_info
        )
        mIvStateStep.setImageResource(R.drawable.ic_open_account_address_auth)
        mBtnOpenAccountNow.text = DynamicResourceManager.getString(
            mContext, string.proof_of_address
        )
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                OpenAccountGuideUtil.gotoPoaAuth(isSupplement = true)
            }
        }
    }

    /**
     * 更新用户补料信息
     */
    private fun updateKycFileInfo(kycFileInfo: UserKYCFileInfo): Boolean {
        if (kycFileInfo.enable) {
            when (kycFileInfo.supplementTypes) {
                "1" -> {
                    //补料身份证明
                    fillPoiAuthUI(false)
                    return true
                }

                "2" -> {
                    if (!kycFileInfo.poiHaveResult) {
                        //展示身份认证审核中
                        updatePoiWaiting()
                    } else {
                        //补料地址证明
                        fillPoaAuthUI()
                    }
                    return true
                }

                "1,2" -> {
                    //补充身份证明和地址证明
                    //如果poi上传完成，则变成上传poa
                    fillPoiAuthUI(true)
                    return true
                }

                else -> {
                    return false
                }
            }
        }
        return false
    }

    /**
     * sumsub身份认证审核中
     */
    private fun updatePoiWaiting() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mIvStateStep.setImageResource(R.drawable.ic_open_account_waiting)
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.complete_auth)
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = false
        mBtnOpenAccountNow.text =
            DynamicResourceManager.getString(mContext, string.poi_auth_waiting)
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_1C1D21))
        mBtnOpenAccountNow.setOnClickListener(null)
    }

    /**
     * 更新身份认证UI（补料）
     */
    private fun fillPoiAuthUI(isContinuePoa: Boolean) {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mTvOpenAccountStateTip.text = DynamicResourceManager.getString(
            mContext, string.supply_info
        )
        mIvStateStep.setImageResource(R.drawable.ic_open_account_auth)
        mBtnOpenAccountNow.text = DynamicResourceManager.getString(
            mContext, string.proof_of_identification
        )
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                OpenAccountGuideUtil.gotoPoiAuth(
                    isSupplement = true,
                    isContinuePoa = isContinuePoa
                )
            }
        }
    }

    /**
     * 更新身份认证和地址认证UI
     */
    private fun updatePoiAuth() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.complete_auth)
        mIvStateStep.setImageResource(R.drawable.ic_open_account_auth)
        mBtnOpenAccountNow.text = DynamicResourceManager.getString(
            mContext, string.proof_of_identification
        )
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                OpenAccountGuideUtil.gotoPoiAuth(isSupplement = false, isContinuePoa = true)
            }
        }
    }

    private fun updatePoaAuth() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.complete_auth)
        mIvStateStep.setImageResource(R.drawable.ic_open_account_address_auth)
        mBtnOpenAccountNow.text = DynamicResourceManager.getString(
            mContext, string.proof_of_address
        )
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = true
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_white))
        mBtnOpenAccountNow.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                OpenAccountGuideUtil.gotoPoaAuth(isSupplement = false)
            }
        }
    }

    /**
     * 更新审核中的UI
     */
    private fun updateWaitingUI() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(this)
        visibility = VISIBLE
        mIvStateStep.setImageResource(R.drawable.ic_open_account_waiting)
        mTvOpenAccountStateTip.text =
            DynamicResourceManager.getString(mContext, string.account_examine)
        mBtnOpenAccountNow.visibility = VISIBLE
        mBtnOpenAccountNow.isEnabled = false
        mBtnOpenAccountNow.text = DynamicResourceManager.getString(mContext, string.examine)
        mBtnOpenAccountNow.setTextColor(mContext.getColor(color.color_1C1D21))
        mBtnOpenAccountNow.setOnClickListener(null)
    }
}